﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Info
{
    public partial class Frm_Supplier : BaseXtraForm
    {
        #region 属性
        
        #endregion

        public Frm_Supplier()
        {
            InitializeComponent();
            this.BindCheckEdit(SupplierView, false, false);

        }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_Supplier_Load(object sender, EventArgs e)
        {
            RepastService aservice = new RepastService();
            treeList1.DataSource = aservice.GetAllSupplierCategory();

            #region TreeList属性
            treeList1.OptionsView.ShowColumns = false;
            treeList1.LookAndFeel.UseWindowsXPTheme = true;
            treeList1.LookAndFeel.UseDefaultLookAndFeel = false;
            treeList1.OptionsView.ShowVertLines = false;
            treeList1.OptionsView.ShowHorzLines = false;
            treeList1.Appearance.FocusedCell.BackColor = Color.LightSteelBlue;
            treeList1.Appearance.FocusedCell.BackColor2 = Color.SteelBlue;
            treeList1.OptionsView.FocusRectStyle = DevExpress.XtraTreeList.DrawFocusRectStyle.None;
            #endregion
        }

        /// <summary>
        /// 右键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeList1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(Control.MousePosition);
            }
        }

        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var categoryID = treeList1.FocusedNode.GetValue("ID");

            Frm_AddSupplier frm = new Frm_AddSupplier(id:(Guid)categoryID,status:EStatus.Add);
            frm.SaveEvent += supplier_AddSaveEvent;
            frm.ShowDialog();
        }
        private void supplier_AddSaveEvent(Object sender,EventArgs e)
        {
            RepastService aservice = new RepastService();
            var focusCategoryID = treeList1.FocusedNode.GetValue("ID");
            SupplierGrid.DataSource = aservice.GetSupplierByCategoryID((Guid)focusCategoryID);
        }

        /// <summary>
        /// 编辑供应商
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var supplier = SupplierView.GetFocusedRow() as Supplier;
            if (supplier == null)
            {
                MessageBox.Show("请选择供应商!");
                return;
            }
            Frm_AddSupplier frm = new Frm_AddSupplier(id:supplier.ID,status:EStatus.Modify);
            frm.SaveEvent += supplier_UpdateSaveEvent;
            frm.ShowDialog();
        }
        private void supplier_UpdateSaveEvent(Object sender,EventArgs e)
        {
            var focusCategoryID = treeList1.FocusedNode.GetValue("ID");
            var supplier = sender as Supplier;
            if (supplier != null)
            {
                SupplierView.SetFocusedRowCellValue("Code", supplier.Code);
                SupplierView.SetFocusedRowCellValue("Name", supplier.Name);
                SupplierView.SetFocusedRowCellValue("Address", supplier.Address);
                SupplierView.SetFocusedRowCellValue("Contracts", supplier.Contracts);
                SupplierView.SetFocusedRowCellValue("Phone", supplier.Phone);
            }
            else
            {
                RepastService aservice = new RepastService();
                SupplierGrid.DataSource = aservice.GetSupplierByCategoryID((Guid)focusCategoryID);
            }
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RepastService aservice = new RepastService();

            //获取分类焦点行ID
            var focusCategoryID = treeList1.FocusedNode.GetValue("ID");

            var supplier = this.GetChecked<Supplier>(SupplierView);

            if (supplier.Count == 0)
            {
                MessageBox.Show("请选择供应商!!!","温馨提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
                return;
            }

            //将流写入Guid数组
            var supplierID = supplier.Select(p => p.ID).ToArray();
            if (DialogResult.OK == MessageBox.Show("确定要删除所选供应商嘛？", "温馨提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                int count = aservice.DelSupplierByCheckedID((Guid)focusCategoryID, supplierID);
                SupplierGrid.DataSource = aservice.GetSupplierByCategoryID((Guid)focusCategoryID);
            }

        }

        /// <summary>
        /// TreeList焦点行改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            RepastService aservice = new RepastService();
            var focusCategoryID = treeList1.FocusedNode.GetValue("ID");
            SupplierGrid.DataSource = aservice.GetSupplierByCategoryID((Guid)focusCategoryID);
        }

    }
}
